<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\FrontendMenu */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<div class="frontend-menu-form">

    <?php $form = ActiveForm::begin([
        'id' => 'clientform'
    ]); ?>

    <?= $form->field($model, 'parent_id')->hiddenInput()->label(false) ?>

    <?= $form->field($model, 'position')->dropDownList(\common\helpers\TStringHelper::getFrontendMenuPosition(), ['prompt' => Yii::t('common', 'select...')])->label(Yii::t('common', 'menu_position')) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'code')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'params')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'icon')->textInput(['id' => 'litpic'])->hint('建议32*32') ?>

    <?= $form->field($model, 'is_single')->dropDownList(\common\helpers\TStringHelper::getYesOrNo()) ?>
    <?= $form->field($model, 'order')->textInput(['type' => 'number']) ?>

    <?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?>

    <div class="form-group text-right">
        <?= Html::button($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'onclick' => $model->isNewRecord ? 'SaveCloseForm();' : 'UpdateForm();']) ?>

        <button type="button" class="btn btn-default" data-dismiss="modal"><?=Yii::t('common', 'close')?></button>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>

    var operation = "";

    var formId = "clientform";

    $("#"+formId).on("submit", function(event) {
        event.preventDefault();
        var validateResult = $("#"+formId).data('yiiActiveForm').validated;
        if (validateResult == true) {
            submitModalForm(formId,"formModal",true,true);
        }

    });

    function FormSubmit()
    {
        $("#"+formId).submit();
    }


    function submitModalForm(formId, modalId,  isClose, isErrorSubmit) {
        var $form = $('#'+formId), THIS = this;
        var actionUrl = $form.attr("action");
        $.post(
            actionUrl,
            $form.serialize()
        )
            .done(function(data) {
                var result = data.result;
                if (result === 'other'){
                    alert(data.message);
                }
                else if (result === 'failure') {
                    if (isErrorSubmit)
                    {
                        $form.submit();
                    }
                    else {
                        alert('操作失败.');
                    }
                }
                else
                {
                    ReloadPageAfterUpdate(modalId, isClose, actionUrl);
                    reloadtree();
                }
                'function' == typeof THIS.callback && THIS.callback();
            })
            .fail(function() {
                alert("内部错误");
            });
        return false;
    }
    //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
    $("#upload_child_attach").change(function(e) {
        var val = e.target.value;
        var point = val.lastIndexOf(".");
        var type = val.substr(point).toLowerCase();
        if(type!=".jpg"&&type!=".jpeg"&&type!=".png"){
            alert('系统目前只支持.jpg、.png文件格式');
            return false;
        }
        $.ajaxFileUpload({
            //处理图片的脚本路径
            url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'menu'])?>',
            type: 'post',       //提交的方式
            secureuri: false,   //是否启用安全提交
            fileElementId: 'upload_child_attach',     //file控件ID
            dataType: 'json',  //服务器返回的数据类型
            success: function (data, status) {  //提交成功后自动执行的处理函数
                if (data.error) {
                    alert(data.msg);
                    return false;
                } else {
                    $("input[id='litpic']").val(data.fileUrl);
                    $("#litpic-div > img").attr('src', data.fileUrl);
                    $("#litpic-div").removeClass('hidden');
                }
            },
            error: function (data, status, e) {   //提交失败自动执行的处理函数
                alert(e);
            }
        })
    })
</script>
